/*
 * 下分按钮
 * author: sfmy
 */
var NNModel           = require('./NNModel');
var NNProto           = require('./NNProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.node.active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit () {
		let canPourScores = NNModel.getCanPourScoresByChairID(NNModel.getMyChairID());
		for (let node of this.node.children) {
			node.active = false;
		} 
		for (let i = 0; i < canPourScores.length; ++i) {
			let node = cc.find('button'+i, this.node);
			if (node) {
				node.active = true;
				cc.find('Label', node).getComponent(cc.Label).string = canPourScores[i]+'分';
			}
		}
		if (NNModel.getGameStatus() == NNProto.gameStatus.POUR_SCORE && NNModel.getMyChairID() != NNModel.getBankerChairID() && NNModel.getPourScoreByChairID(NNModel.getMyChairID()) == 0 && NNModel.getIsPlayingByChairID(NNModel.getMyChairID())) {
			this.node.active = true;
		}
		else {
			this.node.active = false;
		}
	},

	messageCallbackHandler: function(router, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === NNProto.GAME_STATUS_PUSH) {
				if ((msg.data.gameStatus == NNProto.gameStatus.POUR_SCORE) && (NNModel.getMyChairID() != NNModel.getBankerChairID())) {
					if ((NNModel.getGameStatus() == NNProto.gameStatus.POUR_SCORE) && (NNModel.getMyChairID() != NNModel.getBankerChairID()) && NNModel.getIsPlayingByChairID(NNModel.getMyChairID())) {
						this.gameInit();
					}
				}
				else {
					this.node.active = false;
				}
			}
			else if(msg.type === NNProto.GAME_POUR_SCORE_PUSH) {
				if (msg.data.chairID == NNModel.getMyChairID()) {
					this.node.active = false;
				}
			}
		} 
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		let canPourScores = NNModel.getCanPourScoresByChairID(NNModel.getMyChairID());
		let index = parseInt(param);
		let sendData = NNProto.gamePourScoreNotify(canPourScores[index]);
		Global.NetworkManager.notify(GameMessageRouter, sendData);
		this.node.active = false;
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},
});
